package net.xdclass.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.xdclass.request.LockProductRequest;
import net.xdclass.service.ProductService;
import net.xdclass.utils.JsonData;
import net.xdclass.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import springfox.documentation.spring.web.json.Json;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 二当家小D
 * @since 2021-12-29
 */
@Api("商品模块")
@RestController
@RequestMapping("/api/product/v1")
public class ProductController {
    @Autowired
    private ProductService productService;

    @ApiOperation("列出產品列表")
    @GetMapping("page")
    public JsonData pageProductList(
            @ApiParam(value = "當前頁") @RequestParam(value = "page", defaultValue = "1") int page,
            @ApiParam(value = "每頁顯示多少條") @RequestParam(value = "size", defaultValue = "10") int size
    ) {
        Map<String, Object> pageResult = productService.page(page, size);

        return JsonData.buildSuccess(pageResult);
    }

    @ApiOperation("id查詢")
    @GetMapping("detail/{id}")
    public JsonData detail(@ApiParam("id") @PathVariable(value = "id") int id) {
        ProductVO productVO = productService.detail(id);

        return JsonData.buildSuccess(productVO);
    }

    @ApiOperation("商品庫存鎖存")
    @PostMapping("lock_product")
    public JsonData lockProduct(@RequestBody LockProductRequest lockProductRequest) {
        JsonData jsonData = productService.lockProductStock(lockProductRequest);
        return jsonData;
    }

}

